//
//  AlarmViewController.m
//  CarLifes
//
//  Created by yu on 2017/6/30.
//  Copyright © 2017年 jpas. All rights reserved.
//

#import "AlarmViewController.h"
#import "AlarmCell.h"
#import "LabelView.h"
#import "AlarmModel.h"

#import "MJRunningCarHeader.h"

#define kReuseIdentifier @"AlarmCellId"

@interface AlarmViewController ()
<UITableViewDelegate,UITableViewDataSource,DZNEmptyDataSetSource,
DZNEmptyDataSetDelegate>

@property (strong, nonatomic) LabelView *labelView;
@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *dataSource;

@property (nonatomic, assign) NSInteger page;

@property (nonatomic) BOOL isShowEmptyDataSet;

@end

@implementation AlarmViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.extendedLayoutIncludesOpaqueBars = YES;
//    self.edgesForExtendedLayout = UIRectEdgeAll;
    
    if (!self.ownerName.length) {
        self.ownerName = @"告警";
    } else {
        self.ownerName = NSStringFormat(@"告警-%@",self.ownerName);
    }
    self.navigationItem.title = self.ownerName;
    
    self.page = 1;
    
    [self setupLabelView];
    
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewAlarmData)];
    header.lastUpdatedTimeLabel.hidden = YES;
    self.tableView.mj_header = header;
    
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreAlarmData)];
    
    [self loadNewAlarmData];
    
}

// 请求最新的报警数据
- (void)loadNewAlarmData
{
    if (![PPNetworkHelper isNetwork]) {
        [self.tableView.mj_header endRefreshing];
        [MBProgressHUD showToast:@"网络已断开,请检查网络设置"];
        return;
    }
    self.page = 1;
//    NSString *deptId = self.egId.length ? self.egId :
    [self.requestTool loadAlarmsByPage:self.page eqNos:self.eqNo egId:self.egId success:^(NSArray *alarms) {
        self.dataSource = [NSMutableArray arrayWithArray:alarms];
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer resetNoMoreData];
    } failure:^(NSString *error) {
        [self.tableView.mj_header endRefreshing];
        PPLog(@"load alarms error = %@",error);
    }];
}

// 请求更多报警数据
- (void)loadMoreAlarmData
{
    if (![PPNetworkHelper isNetwork]) {
        [self.tableView.mj_footer endRefreshing];
        [MBProgressHUD showToast:@"网络已断开,请检查网络设置"];
        return;
    }
    self.page++;
    [self.requestTool loadAlarmsByPage:self.page eqNos:self.eqNo egId:self.egId success:^(NSArray *alarms) {
        [self.dataSource addObjectsFromArray:alarms];
        [self.tableView reloadData];
        
        if (alarms.count < 10) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        } else {
            [self.tableView.mj_footer endRefreshing];
        }
    } failure:^(NSString *error) {
        [self.tableView.mj_footer endRefreshing];
        PPLog(@"load alarms error = %@",error);
    }];
}

#pragma mark - DZNEmptyDataSetSource,DZNEmptyDataSetDelegate -

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *title = @"暂无告警数据";
    NSDictionary *attributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:16.0f],NSForegroundColorAttributeName:[UIColor darkGrayColor]};
    return [[NSAttributedString alloc] initWithString:title attributes:attributes];
}

- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView*)scrollView {
    return YES;
}

- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView {
    return -50.0;
}

- (void)setupLabelView
{
    [self.view addSubview:self.labelView];
    self.labelView.sd_layout
    .leftEqualToView(self.view)
    .rightEqualToView(self.view)
    .topSpaceToView(self.view, 0);
    
    [self.tableView registerNib:NIB_NAMED(@"AlarmCell") forCellReuseIdentifier:kReuseIdentifier];
    
    [self.view addSubview:self.tableView];
    self.tableView.sd_layout
    .topSpaceToView(self.labelView, 0)
    .leftEqualToView(self.view)
    .rightEqualToView(self.view)
    .bottomSpaceToView(self.view, 0);
}

- (LabelView *)labelView
{
    if (!_labelView) {
        _labelView = [[LabelView alloc] init];
    }
    return _labelView;
}

- (UITableView *)tableView
{
    if (!_tableView)
    {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        //_tableView.backgroundColor = kBackGroundLightGrayColor;
        if (@available(iOS 13.0, *)) {
            UIColor *bgColor = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull traitCollection) {
                if (traitCollection.userInterfaceStyle == UIUserInterfaceStyleLight) {
                    return kBackGroundLightGrayColor;
                } else {
                    return UIColor.systemBackgroundColor;
                }
            }];
            _tableView.backgroundColor = bgColor;
        } else {
            _tableView.backgroundColor = kBackGroundLightGrayColor;
        }
        _tableView.emptyDataSetDelegate = self;
        _tableView.emptyDataSetSource = self;
    }
    return _tableView;
}

#pragma mark - UITableViewDelegate,UITableViewDataSource -

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSource.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 65;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AlarmCell *cell = [tableView dequeueReusableCellWithIdentifier:kReuseIdentifier];
    cell.model = self.dataSource[indexPath.section];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    AlarmModel *model = self.dataSource[indexPath.section];

    if ([model.eqNo isEqualToString:self.eqNo]) return;
    
    AlarmViewController *alarm = [[AlarmViewController alloc] init];
    alarm.eqNo = model.eqNo;
    alarm.ownerName = model.ownerName;
    [self.navigationController pushViewController:alarm animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return !section?1.:kAutoHeight(1.);
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [UIView new];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}


- (NSString *)eqNo
{
    if (!_eqNo) {
        _eqNo = @"";
    }
    return _eqNo;
}

- (NSString *)egId
{
    if (!_egId) {
        _egId = @"";
    }
    return _egId;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}

@end
